package com.example.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.database.DatabaseHelper;
import com.example.function.AddOptionalCourseActivity;
import com.example.pojo.Course;
import com.example.student.R;

import java.util.List;

public class AllCourseAdapter extends RecyclerView.Adapter<AllCourseAdapter.CourseViewHolder> {
    private List<Course> courseList;
    private DatabaseHelper databaseHelper;
    private Context context;

    public AllCourseAdapter(List<Course> courseList, DatabaseHelper databaseHelper,Context context) {
        this.courseList = courseList;
        this.databaseHelper = databaseHelper;
        this.context = context;
    }

    @NonNull
    @Override
    public CourseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_all_course, parent, false);
        return new CourseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CourseViewHolder holder, int position) {
        Course course = courseList.get(position);
        holder.bind(course);

        holder.btnDelete.setOnClickListener(v -> {
            // 弹出确认对话框
            new AlertDialog.Builder(context)
                    .setTitle("删除课程")
                    .setMessage("确定要删除该课程吗？")
                    .setPositiveButton("确定", (dialog, which) -> {
                        // 从列表中移除数据
                        courseList.remove(position);
                        notifyItemRemoved(position);
                        notifyItemRangeChanged(position, courseList.size());

                        databaseHelper.deleteCourse(course.getCourseId());
                        Toast.makeText(holder.itemView.getContext(), "删除成功", Toast.LENGTH_SHORT).show();
                    })
                    .setNegativeButton("取消", null)
                    .show();
        });
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {

            // 创建跳转意图
            Intent intent = new Intent(context, AddOptionalCourseActivity.class);
            intent.putExtra("course_id", course.getCourseId()); // 传递课程ID
            intent.putExtra("course_name", course.getCourseName()); // 传递课程名称
            intent.putExtra("college", course.getCollege()); // 传递学院
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return courseList.size();
    }

    public static class CourseViewHolder extends RecyclerView.ViewHolder {
        private TextView textCourseId, textCourseName, textCollegeMajor, textCreditsSemester;
        private ImageButton btnDelete;

        public CourseViewHolder(@NonNull View itemView) {
            super(itemView);
            textCourseId = itemView.findViewById(R.id.textCourseId);
            textCourseName = itemView.findViewById(R.id.textCourseName);
            textCollegeMajor = itemView.findViewById(R.id.textCollegeMajor);
            textCreditsSemester = itemView.findViewById(R.id.textCreditsSemester);
            btnDelete = itemView.findViewById(R.id.btnDelete);
        }

        public void bind(Course course) {
            textCourseId.setText("课程ID：" + course.getCourseId());
            textCourseName.setText("课程名称：" + course.getCourseName());
            textCollegeMajor.setText("学院：" + course.getCollege() + " 专业：" + course.getMajor());
            textCreditsSemester.setText("学分：" + course.getCredits() + " 学期：" + course.getSemester());
        }
    }
}